package com.situ.allaia.controller;

import com.github.pagehelper.PageInfo;
import com.situ.allaia.common.Global;
import com.situ.allaia.model.*;
import com.situ.allaia.service.CartService;
import com.situ.allaia.service.GoodService;
import com.situ.allaia.util.PaginateInfo;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/good")
public class GoodController {
    private final GoodService goodService;
    private final CartService cartService;

    public GoodController(GoodService goodService, CartService cartService) {
        this.goodService = goodService;
        this.cartService = cartService;
    }

    //跳转到商品列表页
    @GetMapping("/list")
    public String list(GoodSearchBean gsb,
                       Integer categoryId,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "6") Integer limit,
                       Map<String, Object> map, HttpServletRequest req) {
        if (map.get("error")!=null||map.get("error")!=""){
            map.put("error",null);
        }
        String keyword = req.getParameter("keyword");
        if (keyword!=null||keyword!=""){
            gsb.setKeyword(keyword);
        }
        if (categoryId != null) {
            if (gsb.getCategory() == null) {
                gsb.setCategory(new Category());
            }
            gsb.getCategory().setId(categoryId);
            map.put("categoryId", categoryId);
        }

        map.put("keyword", gsb.getKeyword());

        List<Good> goods = goodService.findAll(gsb, new PaginateInfo(page, limit));
        if (goods.size()<=0){
            map.put("error","未查找到相关商品");
            return "list";
        }
        //分页对象
        PageInfo<Good> pi = new PageInfo<>(goods);
        map.put("goods", goods);
        map.put("pi", pi);
        return "list";
    }


    //跳转到商品详情页
    @GetMapping("/detail")
    public String detail(Integer id, Map<String, Object> map, HttpServletRequest req) {
        HttpSession session = req.getSession();
        String idStr = req.getParameter("id1");
        if (idStr!=null&&idStr!=""){
            Integer id1 = Integer.parseInt(idStr);
            id = id1;
        }
        Good good = goodService.findById(id);
        if (good == null) {
            map.put("error", "未找到指定的商品");
            return "error";
        }

        map.put("good", good);
        User user = Global.findUser(session);
        if (user != null){
            List<CartItem> cis = cartService.findByUserId(user.getId());
            map.put("cartItem",cis);
        }

        return "detail";
    }
//    @GetMapping("/error")
//    public String error(){
//        return "error";
//    }
}
